//
//  ViewPointTableviewController.m
//  FloatClouds
//
//  Created by 黄威 on 16/9/8.
//  Copyright © 2016年 黄威. All rights reserved.
//

#import "ViewPointTableviewController.h"
#import "ViewpointDetailCell.h"
#import "HWLNetworkTool.h"
#import "DetailViewPointComments.h"
#import "DetailViewModel.h"


@interface ViewPointTableviewController()
@property (nonatomic, strong) NSArray *arrayOfSectionTitle;
@property (nonatomic, strong) DetailViewModel *detailViewModel;
@property (nonatomic, strong) NSMutableDictionary *heightDic;
@property (nonatomic, strong) NSMutableArray *heightArray;



@end

@implementation ViewPointTableviewController


- (void)viewDidLoad{
    
    self.heightDic = [[NSMutableDictionary alloc] init];
    self.heightArray = [NSMutableArray array];
   
   [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(noti:) name:@"WEBVIEW_HEIGHT" object:nil];
       self.tableView.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height - 44);
    [self handleData];
    [self.tableView registerClass:[ViewpointDetailCell class] forCellReuseIdentifier:@"ViewpointDetailCell"];
   
    self.arrayOfSectionTitle = @[@"   视点资讯",@"   网友评论"];
    [self createNavigation];
}

- (void)createNavigation{
    
    //设置navigation相关属性
    
   
    NSDictionary *attributes=[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName,nil];
    [self.navigationController.navigationBar setTitleTextAttributes:attributes];
    self.navigationItem.title = @"资讯";
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return 1;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.section == 0) {
        ViewpointDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ViewpointDetailCell"];
        cell.tag = indexPath.row;

        cell.contentStr = self.detailViewModel.content;
        cell.labelOfTitle.text = self.detailViewModel.title;
        cell.labelOfTime.text = self.detailViewModel.time;
        cell.labelOfCome_From.text = self.detailViewModel.come_from;
        
        return cell;
        
     
        
    }
    
   
    return nil;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

  
    
    return 1;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{

    return self.arrayOfSectionTitle[section];

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
   
      
        return [[self.heightDic objectForKey:[NSString stringWithFormat:@"%ld",indexPath.row]] floatValue] + 85;
        
 
   
    return 0;
}

- (void)handleData{

   
    NSString *dicOfBody = [@"viewpoint_id="stringByAppendingString:self.viewId];
    NSDictionary *dicOfHeader = @{@"Content-Type":@"application/x-www-form-urlencoded"};
    
    [HWLNetworkTool POST:@"http://app.yanyue.cn/news/detail" andBody:dicOfBody andBodyStyle:HWLBodyString andHeader:dicOfHeader andResponse:HWLJSON andSuccessBlock:^(id result) {
      
        self.detailViewModel = [DetailViewModel modelWithDic:result];
      
        [self.tableView reloadData];
        
    } andFailureBlock:^(NSError *error) {
        
    }];


}

- (void)noti:(NSNotification *)sender
{
    ViewpointDetailCell *cell = [sender object];
    
    
    if (![self.heightDic objectForKey:[NSString stringWithFormat:@"%ld",cell.tag]]||[[self.heightDic objectForKey:[NSString stringWithFormat:@"%ld",cell.tag]] floatValue] != cell.height)
    {
        [self.heightDic setObject:[NSNumber numberWithFloat:cell.height] forKey:[NSString stringWithFormat:@"%ld",cell.tag]];
        [self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:cell.tag inSection:0]] withRowAnimation: UITableViewRowAnimationTop];
    }
}





- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    return 40;
}

@end
